import { Application } from 'egg';
import { isObject } from 'util';

export default (app: Application) => {
    app.beforeStart(async () => {
        //await app.model.sync({ force: true });

        //const ctx = app.createAnonymousContext();
        //await ctx.service.system.initSystem();
    });
    app.validator.addRule('type', (rule, value) => {
        try {
            if (!value || value.length <= 0) {
                return "操作类型不能为空";
            }
            value = Number(value);
            if (value !== 1 && value !== 2) {
                return "无法识别的操作类型";
            }
            console.log(rule);
        } catch (err) {
            return err.message;
        }
    })
    app.validator.addRule('typeThree', (rule, value) => {
        try {
            if (!value || value.length <= 0) {
                return "操作类型不能为空";
            }
            value = Number(value);
            if (value !== 1 && value !== 2 && value !== 3) {
                return "无法识别的操作类型";
            }
            console.log(rule);
        } catch (err) {
            return err.message;
        }
    })
    app.validator.addRule('accusationType', (rule, value) => {
        try {
            if (!value || value.length <= 0) {
                return "操作类型不能为空";
            }
            value = Number(value);
            if (value !== 1 && value !== 2 && value !== 3 && value !== 4) {
                return "无法识别的操作类型";
            }
            console.log(rule);
        } catch (err) {
            return err.message;
        }
    })
    app.validator.addRule('defineArray', ({ rule, value }) => {
        try {
            if (value) {
                if (!isObject(value))
                    value = JSON.parse(value);
                if (Object.prototype.toString.call(value) !== '[object Array]') {
                    return '学院参数格式错误';
                }
            }
            console.log(rule);
        } catch (err) {
            return err.message;
        }
    })
};
